package dp.mobile.eden;

import com.facebook.android.BaseDialogListener;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.FacebookError;
import com.facebook.android.SessionEvents;
import com.facebook.android.SessionStore;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.SessionEvents.AuthListener;
import com.facebook.android.SessionEvents.LogoutListener;

import dp.mobile.system.SoundManager;
import dp.mobile.system.Utilities;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class FacebookAct extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		mLevel	= this.getIntent().getExtras().getString(GameActivityBase.LEVEL_EXTRAS);
		mScores	= this.getIntent().getExtras().getInt(GameActivityBase.SCORE_EXTRAS);
		
		initComp();
		showPostDialog(mLevel, mScores);
	}
	
	private void initComp(){
		m_fb = new Facebook(Utilities.APP_ID);
		m_sessionListener = new SessionListener();
		
		SessionStore.restore(m_fb, this);
		SessionEvents.addAuthListener(m_sessionListener);
		SessionEvents.addLogoutListener(m_sessionListener);
		
		if(!m_fb.isSessionValid()) //If not authorized, authorize it
			fbAuthorize();
	}

	private void fbAuthorize(){
    	m_fb.authorize(this, new LoginDialogListener());
    }
	
	private void showPostDialog(String level, int score){
		Bundle param = new Bundle();
		param.putString("app_id", Utilities.APP_ID);
		param.putString("link", "http://www.facebook.com/apps/application.php?id=115439535156240");
		param.putString("picture", "http://demeister.com/public/media/images/hdpi.png");
		param.putString("name", "Defenders Apocalypse");
		param.putString("caption", "by Dongskar Pedongi");
		param.putString("description", "Defenders Apocalypse. World War III... Mutant Outbreak... Last safe haven on earth");
		param.putString("message", "Just completed level " + level + " with score " + score);
		
    	m_fb.dialog(this, "feed", param, new PostToWallDialogListener());
    }
	
	@Override
	protected void onPause() {
		super.onPause();
		SoundManager.instance(getBaseContext()).temporarySetMute(true);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		SoundManager.instance(getBaseContext()).temporarySetMute(false);
	}
	
	@Override
    public void onActivityResult(int requestCode, int resultCode, android.content.Intent data) {
    	super.onActivityResult(requestCode, resultCode, data);
    	m_fb.authorizeCallback(requestCode, resultCode, data);
    };

	//PROPERTIES
	private Facebook m_fb;
	private SessionListener m_sessionListener;
	private int mScores;
	private String mLevel;
	
	//Inner Listener
    private final class LoginDialogListener implements DialogListener {
        public void onComplete(Bundle values) {
        	Log.d("LoginListener", "on COMPLETE");
            SessionEvents.onLoginSuccess();
        }

        public void onFacebookError(FacebookError error) {
        	Log.d("LoginListener", "on FACEBOOK ERROR");
            SessionEvents.onLoginError(error.getMessage());
        }
        
        public void onError(DialogError error) {
        	Log.d("LoginListener", "on ERROR");
            SessionEvents.onLoginError(error.getMessage());
        }

        public void onCancel() {
        	Log.d("LoginListener", "on CANCEL");
            SessionEvents.onLoginError("Action Canceled");
        }
    }
    
    public class PostToWallDialogListener extends BaseDialogListener {
        public void onComplete(Bundle values) {
            final String postId = values.getString("post_id");
            
            if (postId != null)	Log.d("EdenFB", "onComplete wall post. post_id = " + postId);
            else 				Log.d("EdenFB", "onComplete wall post. No wall post made");
            
            finish();
        }
        
        @Override
        public void onCancel() {
        	super.onCancel();
        	///TODO: shall we do something ?
        }
    }
    
    private class SessionListener implements AuthListener, LogoutListener {
        public void onAuthSucceed() {
        	Log.i("FACEBOOK AUTH", "onAuth SUCCEED");
        	SessionStore.save(m_fb, getBaseContext());
            /// TODO: shall we do something else ?
        }

        public void onAuthFail(String error) {
        	Log.i("FACEBOOK AUTH", "onAuth FAIL = " + error);
        	/// TODO: shall we do something ?
        }
        
        public void onLogoutBegin() {       
        	Log.i("FACEBOOK AUTH", "onLogout BEGIN !!");
        	/// TODO: shall we do something ?
        }
        
        public void onLogoutFinish() {
        	Log.i("FACEBOOK AUTH", "onLougout FINISH !!");
        	SessionStore.clear(getBaseContext());
        	/// TODO: shall we do something else ?
        }
    } 
}
